package com.education.ai.service.impl;

import com.education.ai.entity.Student;
import com.education.ai.entity.Class;
import com.education.ai.repository.StudentRepository;
import com.education.ai.repository.ClassRepository;
import com.education.ai.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {

    private static final Logger log = LoggerFactory.getLogger(StudentServiceImpl.class);

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private ClassRepository classRepository;

    @Override
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    @Override
    public Student findById(Long id) {
        return studentRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("未找到学生，ID: " + id));
    }

    @Override
    @Transactional
    public Student save(Student student) {
        return studentRepository.save(student);
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        studentRepository.deleteById(id);
    }

    @Override
    public List<Student> findByGrade(String grade) {
        return studentRepository.findByGrade(grade);
    }

    @Override
    public List<Student> findByClassId(String classId) {
        return studentRepository.findByClassId(classId);
    }

    @Override
    public List<Student> getStudentsByClassId(String classId, int page, int pageSize) {
        log.info("开始获取班级ID为[{}]的学生列表，页码: {}, 每页大小: {}", classId, page, pageSize);
        
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        log.info("计算的SQL偏移量: {}", offset);
        
        // 1. 首先尝试通过class_id查询
        List<Student> students = studentRepository.findByClassId(classId);
        log.info("通过class_id查询到{}名学生", students.size());
        
        // 2. 如果通过class_id没有找到学生，尝试通过班级名称查询
        if (students.isEmpty()) {
            // 获取班级名称
            Optional<Class> classInfo = classRepository.findById(Long.parseLong(classId));
            if (classInfo.isPresent()) {
                String className = classInfo.get().getClassName();
                log.info("未找到class_id对应的学生，尝试通过班级名称[{}]查询", className);
                students = studentRepository.findByClassName(className);
                log.info("通过班级名称查询到{}名学生", students.size());
            }
        }
        
        // 3. 添加班级信息
        for (Student student : students) {
            student.setClassId(classId);
            Optional<Class> classInfo = classRepository.findById(Long.parseLong(classId));
            if (classInfo.isPresent()) {
                student.setClassName(classInfo.get().getClassName());
            }
        }
        
        // 4. 实现分页
        int start = offset;
        int end = Math.min(offset + pageSize, students.size());
        List<Student> pagedStudents = new ArrayList<>();
        if (start < students.size()) {
            pagedStudents = students.subList(start, end);
        }
        
        log.info("最终返回{}名学生", pagedStudents.size());
        return pagedStudents;
    }
} 